package com.inossem.warehousestorage_inossem.activity.out_storage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.UtilsConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageConfirmBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageOtherBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageOtherConfirmRequestBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.outStorage.OutStorageOtherConfirmRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.library.smoothCheckBox.SmoothCheckBox;
import com.zhy.android.percent.support.PercentRelativeLayout;

/**
 * 作者：wsy on 2017/12/8 10:16
 * <p>
 * 邮箱：445979770@qq.com
 */

public class OutStorageOtherConfirmActivity extends BaseActivity {

    private static final String SUCCESS = "1";//成功

    private TextView title, confirm, submitLine, voucherDate, postDate, isFocusTextView;

    private PercentRelativeLayout dateLayout;

    private SmoothCheckBox isFocus;

    private PercentRelativeLayout submitOverviewLayout;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_other_confirm);
        initView(OutStorageOtherConfirmActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(OutStorageOtherConfirmActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        confirm = (TextView) activity.findViewById(R.id.confirm);
        submitLine = (TextView) activity.findViewById(R.id.submitLine);
        dateLayout = (PercentRelativeLayout) activity.findViewById(R.id.dateLayout);
        voucherDate = (TextView) activity.findViewById(R.id.voucherDate);
        postDate = (TextView) activity.findViewById(R.id.postDate);
        isFocusTextView = (TextView) activity.findViewById(R.id.isFocusTextView);
        isFocus = (SmoothCheckBox) activity.findViewById(R.id.isFocus);
        submitOverviewLayout = (PercentRelativeLayout) activity.findViewById(R.id.submitOverviewLayout);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_change_storage_confirm_submit));
        confirm.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
//                confirm(activity, voucherDate.getText().toString(), postDate.getText().toString(), isFocus.isChecked());
                confirm(activity);
            }
        });
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_receive_add_receiver_title), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        isFocusTextView.setVisibility(View.GONE);
        isFocus.setVisibility(View.GONE);
        isFocus.setOnCheckedChangeListener((SmoothCheckBox checkBox, boolean isChecked) -> {
            if (isChecked) {
                dateLayout.setVisibility(View.VISIBLE);
                voucherDate.setText(Utils.getCurrentStringDate(UtilsConstant.YYYY_MM_DD));
                postDate.setText(Utils.getCurrentStringDate(UtilsConstant.YYYY_MM_DD));
                voucherDate.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        ActivityUtils.getTimePickerDialog(activity, "", voucherDate.getText().toString(), (TimePickerDialog timePickerView, long millseconds) -> {
                            voucherDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                        }).show(getSupportFragmentManager(), "voucherDate");
                    }
                });

                postDate.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        ActivityUtils.getTimePickerDialog(activity, "", postDate.getText().toString(), (TimePickerDialog timePickerView, long millseconds) -> {
                            postDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                        }).show(getSupportFragmentManager(), "postDate");
                    }
                });
            } else {
                dateLayout.setVisibility(View.GONE);
                voucherDate.setText("");
                postDate.setText("");
            }
        });

        getData(activity);
    }

    private void confirm(Context context) {
        OutStorageOtherConfirmRequestBean params = OutStorageUtils.getOutStorageOtherConfirmRequestBean(context);
        OutStorageOtherConfirmRequest.request(context, params, new GsonBeanListener<OutStorageConfirmBean>() {
            @Override
            public void onSuccess(OutStorageConfirmBean bean) {
                SPUtils.removeOtherOutStorageInformation(context);
                if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                    Utils.middleToast(context, bean.getHead().getMsg());
                }
                Intent intent = new Intent(context, OutStorageResultActivity.class);
//                intent.putExtra(OutStorageExtra.MESSAGE, bean.getData().getMsg());
//                intent.putExtra(OutStorageExtra.CONFIRM_MESSAGE, "过账成功");
                startActivity(intent);
            }

            @Override
            public void onFailing(OutStorageConfirmBean bean) {
//                if (!TextUtils.isEmpty(bean.getBody())) {
//                    OutStorageUtils.updateOtherOutStorageNumber(context, bean.getBody());
//                }
            }

        });
    }

    private void getData(Context context) {
        OutStorageOtherBean data = OutStorageUtils.getOutStorageOtherBean(context);
        submitLine.setText(OutStorageUtils.getOtherOutStorageConfirmNumber(context) + "");
        submitOverviewLayout.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getOverviewDialog(context, OutStorageUtils.getOverViewBeanWithOtherOutSorageList(data.getMaterielList())).show();
            }
        });
    }
}
